﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    class MyPanel : Panel
    {
        public static readonly DependencyProperty PaddingProperty =
            DependencyProperty.Register("Padding", 
            typeof (Thickness), 
            typeof (MyPanel), 
            new FrameworkPropertyMetadata(default(Thickness),
                FrameworkPropertyMetadataOptions.AffectsArrange
                | FrameworkPropertyMetadataOptions.AffectsMeasure));

        public Thickness Padding
        {
            get { return (Thickness) GetValue(PaddingProperty); }
            set { SetValue(PaddingProperty, value); }
        }

        public MyPanel()
        {
            ItemHeight = 20;
            ZFactor = 0.8;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            var size = new Size();
            UIElementCollection internalChildren = base.InternalChildren;
            int num = 0;
            int count = internalChildren.Count;
            while (num < count)
            {
                UIElement element = internalChildren[num];
                if (element != null)
                {
                    element.Measure(constraint);
                    var c = element as Control;
                    System.Diagnostics.Debug.WriteLine(c.Name + " "  + element.DesiredSize);
                    size.Width = Math.Max(size.Width, element.DesiredSize.Width);
                    size.Height = Math.Max(size.Height, element.DesiredSize.Height);
                }
                num++;
            }
            return constraint;
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            UIElementCollection elements = base.InternalChildren;
            int num = 0;
            int count = elements.Count;
            while (num < count)
            {
                UIElement element = elements[num];
                if (element != null)
                {
                    element.Arrange(new Rect(arrangeSize));
                }
                num++;
            }
            return arrangeSize;
        }

        private Rect CalculateRect(Size panelSize, int index)
        {
            double zFactor = Math.Pow(ZFactor, index);
            Size itemSize = new Size(panelSize.Width*zFactor, ItemHeight*zFactor);
            double left = (panelSize.Width - itemSize.Width)*0.5;
            double top = panelSize.Height;
            for (int i = 0; i <= index; i++)
            {
                top -= Math.Pow(ZFactor, i)*ItemHeight;
            }
            Rect rect = new Rect(itemSize);
            rect.Location = new Point(left, top);
            return rect;
        }

        protected double ItemHeight { get; set; }

        protected double ZFactor { get; set; }
        
    }
}
